﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PS.WSSDeploy.Contract;

namespace PS.WSSDeploy.Client.Tools
{
    class FileHelper
    {
        public static WSSFiles File2WSSFiles(string sharePointRootFolder, string path, bool withPath)
        {
            if (File.Exists(path))
            {
                var fi = new FileInfo(path);
                var files = new WSSFiles();
                files.Files.Add(CreateWSSFile(sharePointRootFolder, fi, withPath));
                return files;
            }

            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException();
            }
            var entries = new WSSFiles();
            var fileList = new List<WSSFile>();
            FileStreamEntryBuilder(sharePointRootFolder, path, ref fileList, withPath);
            entries.Files = fileList;
            return entries;
        }

        public static WSSFile CreateWSSFile(string sharePointRootFolder, FileInfo fi, bool withPath)
        {
            string entryName = withPath
                ? fi.FullName.Substring(fi.FullName.IndexOf(sharePointRootFolder) + 3)
                : fi.Name;
            return new WSSFile { Name = entryName, Content = File.ReadAllBytes(fi.FullName) };
        }

        public static void FileStreamEntryBuilder(string sharePointRootFolder, string folder, ref List<WSSFile> fileList, bool withPath)
        {
            var dirInfo = new DirectoryInfo(folder);
            
            if (dirInfo.Name == ".svn")
            {
                return;
            }

            var fileInfo = dirInfo.GetFiles();
            foreach(var fi in fileInfo)
            {
                fileList.Add(CreateWSSFile(sharePointRootFolder, fi, withPath));
              
            }
            var childDirInfo = dirInfo.GetDirectories();
            foreach (var di in childDirInfo)
            {
                FileStreamEntryBuilder(sharePointRootFolder, di.FullName, ref fileList, withPath);
            }
        }

        public static WSSFiles Target2WSSFiles(string targetPath, bool withPdb)
        {
            if (!File.Exists(targetPath))
            {
                throw new System.IO.FileNotFoundException();
            }

            var entries = new WSSFiles();
            var fileList = new List<WSSFile>();

            fileList.Add(new WSSFile
                             {
                                 Name = Path.GetFileName(targetPath),
                                 Content = File.ReadAllBytes(targetPath)
                             });

            if(withPdb)
            {
                string pdbName = Path.GetFileNameWithoutExtension(targetPath) + ".pdb";
                string targetPdbPath = Path.Combine(Path.GetDirectoryName(targetPath), pdbName);
                if (File.Exists(targetPdbPath))
                {
                    fileList.Add(new WSSFile
                    {
                        Name = pdbName,
                        Content = File.ReadAllBytes(targetPdbPath)
                    });
                }
            }

            entries.Files = fileList;
            return entries;
        }

        public static WSSFiles TargetPdb2WSSFiles(string folder)
        {
            var entries = new WSSFiles();
            var fileList = new List<WSSFile>();

            var dirInfo = new DirectoryInfo(folder);
            var fileInfo = dirInfo.GetFiles();
            
            foreach (var fi in fileInfo)
            {
                if(fi.Extension == ".pdb")
                {
                    fileList.Add(new WSSFile
                    {
                        Name = fi.Name,
                        Content = File.ReadAllBytes(fi.FullName)
                    });                 
                }
            }
            entries.Files = fileList;
            return entries;
        }

        public static WSSFiles Create(List<RootFile> rootFiles)
        {
            var result = new WSSFiles();
            foreach (var rootFile in rootFiles.Where(rootFile => File.Exists(rootFile.Path)))
            {
                result.Files.Add(new WSSFile
                                     {
                                         Name = rootFile.DeploymentPath,
                                         Content = rootFile.Content ?? File.ReadAllBytes(rootFile.Path)
                                     });
            }
            return result;
        }
    }
}
